package com.imay.live.view;

import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.base.util.StringUtil;
import com.imay.live.activity.dynamics.SearchActivity;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.R;

/**
 * Created by lzx on 2017/05/02 0002.
 */

public class SearchTitle extends RelativeLayout {
    TextView textView;
    EditText editText;
    LinearLayout ll_edit;
    ImageView iv_searchcancle;
    TextWatcher watcher;
    LinearLayout.LayoutParams params;

    public SearchTitle(SearchActivity activity) {
        super(activity);
        initview(activity);
    }

    private void initview(final SearchActivity activity) {
        LayoutInflater.from(activity).inflate(R.layout.search_title, this, true);
        editText = (EditText) findViewById(R.id.et_search);
        ll_edit = (LinearLayout) findViewById(R.id.ll_edit);
        params = (LinearLayout.LayoutParams) editText.getLayoutParams();
        hint = activity.getResources().getString(R.string.search_hint);
        editText.setHint(hint);
        editText.setMaxLines(1);
        editText.clearFocus();
        editText.setSelected(false);
        editText.setFocusable(false);
        editText.setHintTextColor(getResources().getColor(R.color.text_b2));
        iv_searchcancle = (ImageView) findViewById(R.id.iv_searchcancle);
        iv_searchcancle.setVisibility(INVISIBLE);
        textView = (TextView) findViewById(R.id.tv_cancle);
        setBackgroundColor(getResources().getColor(R.color.bg_window));
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (watcher != null) {
                    watcher.beforeTextChanged(s, start, count, after);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (watcher != null) {
                    watcher.onTextChanged(s, start, before, count);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    iv_searchcancle.setVisibility(VISIBLE);
                } else {
                    iv_searchcancle.setVisibility(INVISIBLE);
                }
                if (watcher != null) {
                    watcher.afterTextChanged(s);
                }
            }
        });
        editText.setInputType(InputType.TYPE_CLASS_TEXT);
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        ll_edit.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    softInputMode(activity);
                }
                return false;
            }
        });
        editText.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    MobclickAgent.onEvent(activity, "search_click");
                    softInputMode(activity);
                }
                return false;
            }
        });
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    activity.searchText(getEditText().getText().toString());
                    return true;
                }
                return false;
            }
        });
        iv_searchcancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isEmpty(editText.getText().toString())) {
                    editText.setCursorVisible(false);
                    params.weight = 0;
                } else {
                    editText.setText("");
                    params.weight = 1;
                }
                editText.setHint(getResources().getString(R.string.search_hint));
                editText.setLayoutParams(params);
                InputMethodManager inputManager =
                        (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                IBinder binder = editText.getWindowToken();
                if (binder != null) {
                    inputManager.hideSoftInputFromWindow(binder, 0);
                }

            }
        });
        softInputMode(activity);//产品后来要求进入就弹输入法
    }

    private void softInputMode(Activity activity) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        setCursorVisible();
        editText.requestFocus();
    }

    String hint;

    public EditText getEditText() {
        return editText;
    }

    public void setCursorVisible() {
        editText.setCursorVisible(true);// 再次点击显示光标
        editText.setHint(hint);
        params.weight = 1;
        editText.setLayoutParams(params);
    }

    public void setHint(String hint) {
        this.hint = hint;
        editText.setHint(hint);
    }

    public void setmindle() {
        if (editText.getText().toString().length() == 0) {
            editText.setHint(editText.getText().length() > 0 ? "" : hint);
            params.weight = 0;
            editText.setLayoutParams(params);
        }
        editText.clearFocus();
        editText.clearFocus();
        editText.setSelected(false);
        editText.setFocusable(false);
        editText.setFocusableInTouchMode(false);
        editText.setCursorVisible(false);// 再次点击显示光标
    }

    public void setTextWatcher(TextWatcher watcher) {
        this.watcher = watcher;
    }

    public void setCancleOnClickListener(OnClickListener onClickListener) {
        textView.setOnClickListener(onClickListener);
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) getResources().getDimension(R.dimen.title_bar_height) + getStatusBarHeight(), MeasureSpec.EXACTLY));
    }
}
